`
https://leetcode.cn/problems/balanced-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function (root) {
  let isBal = true

  const traverse = (root) => {
    if (root === null || !isBal) return 0

    const leftMaxCount = traverse(root.left)
    const rightMaxCount = traverse(root.right)

    if (Math.abs(leftMaxCount - rightMaxCount) > 1) {
      isBal = false
    }

    return Math.max(leftMaxCount, rightMaxCount) + 1
  }

  traverse(root)

  return isBal
};